package com.yiwei.hdb.adapter;

import android.os.Parcelable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.PagerAdapter;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by ezy on 14-8-11.
 */
public class TabsAdapter extends PagerAdapter {

    private static final String TAG = "ezy";
    private static final boolean DEBUG = true;

    private final FragmentManager _fm;
    private final String[] _titles;
    private Fragment[] _fragments;

    private FragmentTransaction _currentTransaction = null;
    private Fragment _currentPrimaryItem = null;

    public TabsAdapter(FragmentManager fm, Fragment[] fragments, String[] titles) {
        _fm = fm;
        _fragments = fragments;
        if (titles == null) {
            _titles = new String[fragments.length];
            for (int i = 0; i < fragments.length; i++) {
                _titles[i] = fragments[i].toString();
            }
        } else {
            _titles = titles;
        }
    }
    public TabsAdapter(FragmentManager fm, Fragment[] fragments) {
        this(fm, fragments, null);
    }
    @Override
    public CharSequence getPageTitle(int position) {
        return _titles[position];
    }

    @Override
    public int getCount() {
        return _fragments.length;
    }

    public long getItemId(int position) {
        return _titles[position].hashCode();
    }


    private void log(String content) {
        if (DEBUG) Log.v(TAG, content);
    }
    private static String nameIt(int viewId, long id) {
        return "ezy:tabs:" + viewId + ":" + id;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        if (_currentTransaction == null) {
            _currentTransaction = _fm.beginTransaction();
        }

        final long itemId = getItemId(position);

        Fragment fragment = _fragments[position];
        if (!fragment.isAdded()) {
            _currentTransaction.add(container.getId(), fragment, nameIt(container.getId(), itemId));
            log("Adding item #" + itemId + ": f=" + fragment);
        }
        _currentTransaction.show(fragment);
        if (fragment != _currentPrimaryItem) {
            fragment.setMenuVisibility(false);
            fragment.setUserVisibleHint(false);
        }

        return fragment;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        if (_currentTransaction == null) {
            _currentTransaction = _fm.beginTransaction();
        }
        log("hide item #" + getItemId(position) + ": f=" + object + " v=" + ((Fragment)object).getView());
//        _currentTransaction.remove((Fragment) object);
        _currentTransaction.hide((Fragment) object);
    }

    @Override
    public void setPrimaryItem(ViewGroup container, int position, Object object) {
        Fragment fragment = (Fragment)object;
        if (fragment != _currentPrimaryItem) {
            if (_currentPrimaryItem != null) {
                _currentPrimaryItem.setMenuVisibility(false);
                _currentPrimaryItem.setUserVisibleHint(false);
            }
            if (fragment != null) {
                fragment.setMenuVisibility(true);
                fragment.setUserVisibleHint(true);
            }
            _currentPrimaryItem = fragment;
        }
    }

    @Override
    public void finishUpdate(ViewGroup container) {
        if (_currentTransaction != null) {
            _currentTransaction.commitAllowingStateLoss();
            _currentTransaction = null;
            _fm.executePendingTransactions();
        }
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return ((Fragment)object).getView() == view;
    }

    @Override
    public Parcelable saveState() {
        return null;
    }

    @Override
    public void startUpdate(ViewGroup container) {
    }
    @Override
    public void restoreState(Parcelable state, ClassLoader loader) {
    }


}
